package com.qa.dom.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qa.dom.dto.HomePageDto;
import com.qa.dom.dto.notice.AttentionTeacherReq;
import com.qa.dom.entity.Notice;
import com.qa.dom.entity.Usertable;
import com.qa.dom.service.INoticeService;
import com.qa.dom.service.IUsertableService;
import com.qa.dom.util.JsonResult;
import com.qa.dom.util.baseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;

/**
 * 收藏（关注表）管理
 * @author jiujichina
 * @since 2017-12-25
 */
@RestController   //注解告诉spring将生成的字符串直接返回给调用者
//@Api将类标记为Swagger资源
@RequestMapping("/notice")  //配置url映射
@Api(description = "收藏（关注表）管理",tags = {"NOTICE"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class NoticeController extends baseAction {
        @Resource
        private INoticeService noticeService;

        /*-------------------------------- 关注老师----------------------------------*/
        @PostMapping("/attentionTeacher")
        @ApiOperation(value = "关注老师",notes = "关注老师",httpMethod = "POST")
        public JsonResult attentionTeacher(@RequestBody AttentionTeacherReq attentionTeacherReq){
            //Usertable usertable = getCurrentUser();
            EntityWrapper<Notice> wrapper = new EntityWrapper<>();
            wrapper.eq("bynoticer",attentionTeacherReq.getTeacherId());
            wrapper.eq("noticer",attentionTeacherReq.getUserId());
            List<Notice> notices = noticeService.selectList(wrapper);
            System.out.println("<<<<<"+notices.size());
            if(notices.size()!=0){
                return renderError("你已经关注过此老师");
            }
            Notice newNotice = new Notice();
            newNotice.setNoticer(attentionTeacherReq.getUserId());
            newNotice.setBynoticer(attentionTeacherReq.getTeacherId());
            newNotice.setCreateDate(new Date());
            System.out.println("<<<<<<"+notices+"\n"+newNotice);
            return noticeService.insert(newNotice)?renderSuccess("关注成功"):renderError("关注失败！！！");
        }
}





